package com.jinlejia.erp.ui.mine.contract

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.TypeReference
import com.jinlejia.erp.R
import com.jinlejia.erp.adapter.FollowUpDetailsAdapter
import com.jinlejia.erp.base.ApiConfig
import com.jinlejia.erp.base.MapCallBack
import com.jinlejia.erp.databinding.ActivityFollowUpDetailsListBinding
import com.jinlejia.erp.utils.DataFilter
import com.zhy.http.okhttp.OkHttpUtils
import okhttp3.Call

/***
 * 跟进详情列表Activity
 */
class FollowUpDetailsListActivity : AppCompatActivity() {
    val binding:ActivityFollowUpDetailsListBinding by lazy {
        DataBindingUtil.setContentView<ActivityFollowUpDetailsListBinding>(this,R.layout.activity_follow_up_details_list)
    }
    lateinit var data:ArrayList<HashMap<String,Any>>
    lateinit var adapter:FollowUpDetailsAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_follow_up_details_list)
        initView()
        getData(intent.getIntExtra("trackId",0))
    }

    private fun initView() {
        data = ArrayList()
        binding.backBtn.setOnClickListener { finish() }

        adapter = FollowUpDetailsAdapter(data)
        binding.recycler.layoutManager = LinearLayoutManager(this)
        binding.recycler.adapter = adapter
        adapter.setItemListener(object : FollowUpDetailsAdapter.OnItemListener{
            override fun onClick(map: HashMap<String, Any>) {
                val intent = Intent(baseContext,FollowUpDetailsActivity::class.java)
                intent.putExtra("content", map["followContent"].toString())
                startActivity(intent)
            }
        })
    }

    private fun getData(trackId: Int) {

        OkHttpUtils
            .get()
            .url(ApiConfig.Follow_Up_DetailsList + trackId)
            .build()
            .execute(object : MapCallBack(){
                override fun onResponse(response: MutableMap<String, Any>?, id: Int) {
                    val filter = DataFilter.filter(baseContext,response,adapter)
                    if (filter){
                        data = JSON.parseObject(response!!["data"].toString(),object :
                            TypeReference<java.util.ArrayList<HashMap<String, Any>>>() {})
                        adapter.data = data
                    }
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                    e?.printStackTrace()
                }

            })
    }
}
